using System;
using System.Collections.Generic;
using System.Text;
using Castle.MonoRail.Framework;
using System.Web.Security;
using System.Reflection;

namespace Stormwind.Controllers {
	/// <summary>
	/// Controller that manages authentication actions.
	/// </summary>
	public class GenericController : SmartDispatcherController {

		#region Test Xml
		string xmlData = @"
<ItemSearchResponse xmlns=""http://webservices.amazon.com/AWSECommerceService/2006-06-28"">
    <OperationRequest>
        <HTTPHeaders>
            <Header Name=""UserAgent""
                    Value=""Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser; Avant Browser; .NET CLR 1.0.3705; .NET CLR 2.0.50727; .NET CLR 1.1.4322; Media Center PC 4.0; InfoPath.2)""></Header>
        </HTTPHeaders>
        <RequestId>18CZWZFXKSV8F601AGMF</RequestId>
        <Arguments>
            <Argument Name=""Service"" Value=""AWSECommerceService""></Argument>
            <Argument Name=""AssociateTag"" Value=""ws""></Argument>
            <Argument Name=""SearchIndex"" Value=""Books""></Argument>
            <Argument Name=""Author"" Value=""Sidney Sheldon""></Argument>
            <Argument Name=""SubscriptionId"" Value=""1A7XKHR5BYD0WPJVQEG2""></Argument>
            <Argument Name=""Version"" Value=""2006-06-28""></Argument>
            <Argument Name=""Operation"" Value=""ItemSearch""></Argument>
        </Arguments>
        <RequestProcessingTime>1.05041599273682</RequestProcessingTime>
    </OperationRequest>
    <Items>
        <Request>
            <IsValid>True</IsValid>
            <ItemSearchRequest>
                <Author>Sidney Sheldon</Author>
                <SearchIndex>Books</SearchIndex>
            </ItemSearchRequest>
        </Request>
        <TotalResults>203</TotalResults>
        <TotalPages>21</TotalPages>
        <Item>
            <ASIN>0446355453</ASIN>
            <DetailPageURL>
                http://www.amazon.com/gp/redirect.html%3FASIN=0446355453%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0446355453%253FSubscriptionId=1A7XKHR5BYD0WPJVQEG2
            </DetailPageURL>
            <ItemAttributes>
                <Author>Sidney S123heldon</Author>
                <Manufacturer>Warner Books</Manufacturer>
                <ProductGroup>Book</ProductGroup>
                <Title>Master of the qwerty</Title>
            </ItemAttributes>
        </Item>
        <Item>
            <ASIN>0446613657</ASIN>
            <DetailPageURL>
                http://www.amazon.com/gp/redirect.html%3FASIN=0446613657%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0446613657%253FSubscriptionId=1A7XKHR5BYD0WPJVQEG2
            </DetailPageURL>
            <ItemAttributes>
                <Author>Sidney Sheldon</Author>
                <Manufacturer>Warner Books</Manufacturer>
                <ProductGroup>Book</ProductGroup>
                <Title>Are You Afraid 789of the Dark?</Title>
            </ItemAttributes>
        </Item>
        <Item>
            <ASIN>0446357421</ASIN>
            <DetailPageURL>
                http://www.amazon.com/gp/redirect.html%3FASIN=0446357421%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0446357421%253FSubscriptionId=1A7XKHR5BYD0WPJVQEG2
            </DetailPageURL>
            <ItemAttributes>
                <Author>Sidney Sheldon</Author>
                <Manufacturer>Warner Books</Manufacturer>
                <ProductGroup>Book</ProductGroup>
                <Title>If Tomorrow Comes</Title>
            </ItemAttributes>
        </Item>
        <Item>
            <ASIN>0446607207</ASIN>
            <DetailPageURL>
                http://www.amazon.com/gp/redirect.html%3FASIN=0446607207%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0446607207%253FSubscriptionId=1A7XKHR5BYD0WPJVQEG2
            </DetailPageURL>
            <ItemAttributes>
                <Author>Sidney Sheldon</Author>
                <Manufacturer>Warner Vision</Manufacturer>
                <ProductGroup>Book</ProductGroup>
                <Title>Tell Me Your Dreams</Title>
            </ItemAttributes>
        </Item>
        <Item>
            <ASIN>0446357448</ASIN>
            <DetailPageURL>
                http://www.amazon.com/gp/redirect.html%3FASIN=0446357448%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0446357448%253FSubscriptionId=1A7XKHR5BYD0WPJVQEG2
            </DetailPageURL>
            <ItemAttributes>
                <Author>Sidney Sheldon</Author>
                <Manufacturer>Warner Books</Manufacturer>
                <ProductGroup>Book</ProductGroup>
                <Title>Bloodline</Title>
            </ItemAttributes>
        </Item>
        <Item>
            <ASIN>0446532673</ASIN>
            <DetailPageURL>
                http://www.amazon.com/gp/redirect.html%3FASIN=0446532673%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0446532673%253FSubscriptionId=1A7XKHR5BYD0WPJVQEG2
            </DetailPageURL>
            <ItemAttributes>
                <Author>Sidney Sheldon</Author>
                <Manufacturer>Warner Books</Manufacturer>
                <ProductGroup>Book</ProductGroup>
                <Title>The Other Side of Me</Title>
            </ItemAttributes>
        </Item>
        <Item>
            <ASIN>0446356573</ASIN>
            <DetailPageURL>
                http://www.amazon.com/gp/redirect.html%3FASIN=0446356573%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0446356573%253FSubscriptionId=1A7XKHR5BYD0WPJVQEG2
            </DetailPageURL>
            <ItemAttributes>
                <Author>Sidney Sheldon</Author>
                <Manufacturer>Warner Books</Manufacturer>
                <ProductGroup>Book</ProductGroup>
                <Title>A Stranger in the Mirror</Title>
            </ItemAttributes>
        </Item>
        <Item>
            <ASIN>0060198346</ASIN>
            <DetailPageURL>
                http://www.amazon.com/gp/redirect.html%3FASIN=0060198346%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0060198346%253FSubscriptionId=1A7XKHR5BYD0WPJVQEG2
            </DetailPageURL>
            <ItemAttributes>
                <Author>Sidney Sheldon</Author>
                <Manufacturer>William Morrow &amp; Company</Manufacturer>
                <ProductGroup>Book</ProductGroup>
                <Title>The Sky Is Falling</Title>
            </ItemAttributes>
        </Item>
        <Item>
            <ASIN>0446354732</ASIN>
            <DetailPageURL>
                http://www.amazon.com/gp/redirect.html%3FASIN=0446354732%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0446354732%253FSubscriptionId=1A7XKHR5BYD0WPJVQEG2
            </DetailPageURL>
            <ItemAttributes>
                <Author>Sidney Sheldon</Author>
                <Manufacturer>Warner Books</Manufacturer>
                <ProductGroup>Book</ProductGroup>
                <Title>Nothing Lasts Forever</Title>
            </ItemAttributes>
        </Item>
        <Item>
            <ASIN>0446341916</ASIN>
            <DetailPageURL>
                http://www.amazon.com/gp/redirect.html%3FASIN=0446341916%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0446341916%253FSubscriptionId=1A7XKHR5BYD0WPJVQEG2
            </DetailPageURL>
            <ItemAttributes>
                <Author>Sidney Sheldon</Author>
                <Manufacturer>Warner Books</Manufacturer>
                <ProductGroup>Book</ProductGroup>
                <Title>The Naked Face</Title>
            </ItemAttributes>
        </Item>
    </Items>
</ItemSearchResponse>
			";
		#endregion 

		public string AssemblyName {
			get {
				return Request.QueryString["assembly"];
			}
		}

		public string ClassName {
			get {
				return Request.QueryString["class"];
			}
		}

		public string Method {
			get {
				return Request.QueryString["method"];
			}
		}

		public int CurrentIndex {
			get {
				return Convert.ToInt32(Request.Form["start"]);
			}
		}

		public int NumberOfItems {
			get {
				return Convert.ToInt32(Request.Form["limit"]);
			}
		}

		/// <summary>
		/// Gets XML from a business rule.
		/// </summary>
		public void GetXml() {
			this.Response.ContentType = "text/xml";
			this.Response.CachePolicy.SetCacheability(System.Web.HttpCacheability.NoCache);
			this.Response.Clear();

			try {
				if (ClassName.Equals("Test", StringComparison.InvariantCultureIgnoreCase) && Method.Equals("Test", StringComparison.InvariantCultureIgnoreCase)) {
					PropertyBag["xmlData"] = xmlData;
				}
				else {
					PropertyBag["xmlData"] = ExecuteMethod();
				}
			}
			catch (System.Exception ex) {
				throw ex;
			}
		}

		private string ExecuteMethod() {
			string fullName = string.Format("{0}, {1}", ClassName, AssemblyName);
			Type tp = Type.GetType(fullName);
			if (tp == null) {
				throw new ArgumentNullException("Type not found: " + fullName);
			}
			MethodInfo mi = tp.GetMethod(Method);

			if (mi == null) {
				throw new ArgumentNullException("Method not found: " + Method);
			}

			if (mi.ReturnType != typeof(string)) {
				throw new ArgumentOutOfRangeException("This method does not return a string.");
			}

			string ret = null;
			object[] prms = new object[] { CurrentIndex, NumberOfItems };
			if (mi.IsStatic) {
				ret = Convert.ToString(mi.Invoke(null, prms));
			}
			else {
				object obj = Activator.CreateInstance(tp);
				ret = Convert.ToString(mi.Invoke(obj, prms));
			}

			return ret;
		}
	}
}
